﻿using System.Collections.Generic;
using CarlosAg.ExcelXmlWriter;
using SdmxMl.Common;
using SdmxMl.Structure;
using SdmxMl.Manager;

namespace SdmxMl.Excel
{
    /// <summary>
    /// Specialized class for generating Excel export of CategoryScheme 
    /// </summary>
    public class ExcelExportCategoryScheme : ExcelExportBase
    {
        #region Internal members

        private CategorySchemeType cst;
        private int itemCount;

        #endregion

        #region C'tor

        public ExcelExportCategoryScheme(CategorySchemeType c)
        {
            cst = c;
        }
        #endregion

        #region Header

        private void GenerateHeaderNames()
        {
            headerColitems = new List<string>(16);

            headerColitems.Add("Concept");
            headerColitems.Add("Parent");
            headerColitems.Add("Level");

            foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                headerColitems.Add("Name_" + lg);

            foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                headerColitems.Add("Desc_" + lg);

            headerColitems.Add("Urn");

            anInventory = new AnnotationInventory();
            foreach (Category it in cst.CategoryList)
                FillAnnot(it);

            anInventory.GenerateAnnotationHeader(headerColitems);
        }
        #endregion

        #region Overriden Export Methods

        protected void FillAnnot(Category item)
        {
            anInventory.Inventory(item.AnnotationList, false);
            foreach (Category it in item.CategoryList)
                FillAnnot(it);
        }


        public override void Export()
        {
            GenerateBook("SMS", "OFS");
            GenerateWorksheetArtefact(cst);
            GenerateHeaderNames();

            sheet = GenerateWorksheetHeader(
                headerColitems.ToArray(), "Categories", cst.CategoryList.Count);

            itemCount = 0;
            CreateRows(cst.CategoryList);

            // Update sheet count
            sheet.Table.ExpandedRowCount = 1 + itemCount;

        }


        public void CreateRows(CategoryList categoryList)
        {
            itemCount += categoryList.Count;
            foreach (Category it in categoryList)
                FillRow(null, it, 0);
        }


        protected void FillRow(Category parent, Category item, int level)
        {
            WorksheetRow row = sheet.Table.Rows.Add();
            AddCell(row, item.Id);
            AddCell(row, parent != null ? parent.FullId : string.Empty);
            AddCell(row, (level + 1).ToString());

            foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                AddCell(row, item.Name.GetIfDefinedForLanguage(lg));

            MultiLang desc = item.Description;
            if (desc != null && desc.IsEmpty == false)
            {
                foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                    AddCell(row, desc.GetIfDefinedForLanguage(lg));
            }
            else
            {
                foreach (string lg in SdmxMl.Common.LangSupport.SupportedLanguages)
                    AddCell(row, string.Empty);
            }

            // Urn
            AddCell(row, UrnManager.FullIdToUrn(SdmxArtefactType.CategorySchemes, cst.FullIdent, item, null));

            // All annotations
            AddAnnotations(row, item.AnnotationList);

            // Recurse
            itemCount += item.CategoryList.Count;
            foreach (Category it in item.CategoryList)
                FillRow(item, it, level + 1);
        }
        #endregion

    }
}
